package com.xjj.serviceattend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Attend;
import com.xjj.entity.Student;
import com.xjj.mapper.StudentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Auther: AYang
 * @Date: 2022/10/20 10:49
 * @Description:
 */
@Repository
public interface AttendMapper extends BaseMapper<Attend> {
    /**
     * 根据班级id查询今日考勤学生列表
     * @param klassId
     * @return
     */
    @Select("select name,gender,sid,attend_status,tel from xjj_student where klass_id = #{klassId}")
    List<Student> getStuByKid(@Param("klassId") Integer klassId);

    /**
     * 修改学生考勤状态
     * @param id
     * @param attendStatus
     */
    @Update("update xjj_student set attend_status = #{attendStatus} where id=#{id}")
    void updateAttendStatusBySid(@Param("id")Integer id,@Param("attendStatus")String attendStatus);

    @Select("select date from xjj_attend where date = #{date}")
    String getDateByDate(String date);

    /**
     * 根据考勤状态和班级统计当前班级考勤状态人数
     * @param attendStatus
     * @return
     */
    @Select("SELECT COUNT(*) FROM xjj_student WHERE attend_status = #{attendStatus} and klass_id = #{klassId}")
    Integer getAttendStatusNum(@Param("attendStatus")String attendStatus,@Param("klassId") Integer klassId);

    @Select("select name from xjj_klass where master_id = #{masterId};")
    String getClassNameByMasterId(@Param("masterId") Integer masterId);

    @Select("select id from xjj_klass where master_id = #{masterId};")
    Integer getClassIdByMasterId(@Param("masterId") Integer masterId);

    @Update("update xjj_student set attend_status = #{attendStatus} where klass_id=#{klassId}")
    void updateAttendStatusByKlassId(@Param("klassId")Integer klassId,@Param("attendStatus")String attendStatus);

    @SelectProvider(type = AttendMapper.AttendSqlProvider.class, method = "select")
    @Results(id = "attendMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "klass_id", property = "klassId"),
            @Result(column = "attended_num", property = "attendedNum"),
            @Result(column = "absence_num", property = "absenceNum"),
            @Result(column = "late_num", property = "lateNum"),
            @Result(column = "date", property = "date"),
    })
    List<Attend> getByCondition(Attend attend);

    /**
     * 多条件更新数据
     * @param attend
     */
    @UpdateProvider(type = AttendMapper.AttendSqlProvider.class, method = "update")
    void updateByCondition(Attend attend);

    @Slf4j
    class AttendSqlProvider {
        /**
         * 动态更新sql语句
         * @param attend
         * @return
         */
        public String update(Attend attend) {
            return new SQL() {
                {
                    UPDATE("xjj_attend");
                    if (attend.getKlassId() != null) {
                        SET("klass_id=#{klassId}");
                    }
                    if (attend.getAttendedNum() != null) {
                        SET("attended_num=#{attendedNum}");
                    }
                    if (attend.getAbsenceNum() != null) {
                        SET("absence_num=#{absenceNum}");
                    }
                    if (attend.getLateNum() != null) {
                        SET("late_num=#{lateNum}");
                    }
                    if (StringUtils.hasLength(attend.getDate())) {
                        SET("date=#{date}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
        }

        /**
         * 得到动态产生的多条件查询 sql 语句
         *
         * @param attend
         * @return 动态产生的 sql 查询语句
         */
        public String select(Attend attend) {
            return new SQL() {
                {
                    SELECT("a.*,k.name,k.klass_num").FROM("xjj_attend a");
                    LEFT_OUTER_JOIN("xjj_klass k on k.id = a.klass_id");
                    if (attend.getKlassId() != null) {
                        WHERE("a.klass_id=#{klassId}");
                    }
                    if (attend.getAttendedNum() != null) {
                        WHERE("a.attended_num=#{attendedNum}");
                    }
                    if (attend.getAbsenceNum() != null) {
                        WHERE("a.absence_num=#{absenceNum}");
                    }
                    if (attend.getLateNum() != null) {
                        WHERE("a.late_num=#{lateNum}");
                    }
                    if (StringUtils.hasLength(attend.getDate())) {
                        WHERE("a.date=#{date}");
                    }
                }
            }.toString();
        }
    }
}
